﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsVote
    {
        public int Add(KeRui.Model.CmsVote model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@SiteID", SqlDbType.Int, 4), new SqlParameter("@VoteType", SqlDbType.Int, 4), new SqlParameter("@VoteLimit", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@Content", SqlDbType.NText), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.SiteID;
            parameters[2].Value = model.VoteType;
            parameters[3].Value = model.VoteLimit;
            parameters[4].Value = model.Title;
            parameters[5].Value = model.Content;
            parameters[6].Value = model.CreateIP;
            DbHelperSQL.RunProcedure("CmsVote_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int VoteID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteID;
            DbHelperSQL.RunProcedure("CmsVote_Delete", parameters, out num);
        }

        public bool Exists(int VoteID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteID;
            return (DbHelperSQL.RunProcedure("CmsVote_Exists", parameters, out num) == 1);
        }

        public KeRui.Model.CmsVote GetModel(int VoteID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteID;
            KeRui.Model.CmsVote vote = new KeRui.Model.CmsVote();
            DataSet set = DbHelperSQL.RunProcedure("CmsVote_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["VoteID"].ToString() != "")
                {
                    vote.VoteID = int.Parse(set.Tables[0].Rows[0]["VoteID"].ToString());
                }
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    vote.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                if (set.Tables[0].Rows[0]["VoteType"].ToString() != "")
                {
                    vote.VoteType = int.Parse(set.Tables[0].Rows[0]["VoteType"].ToString());
                }
                if (set.Tables[0].Rows[0]["VoteLimit"].ToString() != "")
                {
                    vote.VoteLimit = int.Parse(set.Tables[0].Rows[0]["VoteLimit"].ToString());
                }
                vote.VoteNum = int.Parse(set.Tables[0].Rows[0]["VoteNum"].ToString());
                vote.Title = set.Tables[0].Rows[0]["Title"].ToString();
                vote.Content = set.Tables[0].Rows[0]["Content"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    vote.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                vote.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                return vote;
            }
            return null;
        }

        public void Update(KeRui.Model.CmsVote model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@VoteType", SqlDbType.Int, 4), new SqlParameter("@VoteLimit", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@Content", SqlDbType.NText) };
            parameters[0].Value = model.VoteID;
            parameters[1].Value = model.VoteType;
            parameters[2].Value = model.VoteLimit;
            parameters[3].Value = model.Title;
            parameters[4].Value = model.Content;
            DbHelperSQL.RunProcedure("CmsVote_Update", parameters, out num);
        }

        public void UpdateSum(int VoteID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteID;
            DbHelperSQL.RunProcedure("CmsVote_UpdateNum", parameters, out num);
        }
    }
}

